package com.wcl.demo.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.easemob.chat.EMChatManager;
import com.wcl.demo.R;
import com.wcl.demo.db.AroundHelper;
import com.wcl.demo.db.NoteBgHelper;
import com.wcl.demo.model.entity.AroundInfor;
import com.wcl.demo.model.entity.NoteBgData;
import com.wcl.demo.network.VolleySingleton;
import com.wcl.demo.ui.adapter.NoteAdapter;
import com.wcl.demo.ui.fragment.NoteBgDialogFragment;
import com.wcl.demo.ui.fragment.NoteDialogFragment;
import com.wcl.demo.utils.PreferenceManager;

import java.util.List;

/**
 * Created by dllo on 15/11/24.
 */
public class NoteActivity extends FragmentActivity implements View.OnClickListener {
    private ImageView activity_note_list_bac_Iv;//返回按钮
    private ImageView note_take_photo_Iv, not_bg_Img, note_head_pic_Iv;//定义拍照,头像,头像背景墙照片
    private TextView currentUsername, usernickname;//定义当前用户名,昵称
    private List<AroundInfor> list;
    private AroundHelper helper;
    private NoteAdapter adapter;
    private ListView note_listView;
    private View header;
    private NoteBgHelper noteBgHelper;
    private List<NoteBgData> noteBgDataLists;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();//定义Volley请求


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        initView();
        initData();
    }

    /***
     * 初始化控件
     */
    private void initView() {

        activity_note_list_bac_Iv = (ImageView) findViewById(R.id.activity_note_list_bac_Iv);//返回按钮
        activity_note_list_bac_Iv.setOnClickListener(this);

        note_listView = (ListView) findViewById(R.id.note_listView);//发送的心情
        header = LayoutInflater.from(this).inflate(R.layout.note_header, null);
        note_take_photo_Iv = (ImageView) header.findViewById(R.id.note_take_photo_Iv);//拍照按钮
        note_listView.addHeaderView(header);//给ListView加头

        not_bg_Img = (ImageView) header.findViewById(R.id.not_bg_Img);//背景墙
        not_bg_Img.setOnClickListener(this);
        currentUsername = (TextView) header.findViewById(R.id.currentUsername);//当前用户名
        usernickname = (TextView) header.findViewById(R.id.usernickname);//昵称
        note_head_pic_Iv = (ImageView) header.findViewById(R.id.note_head_pic_Iv);//头像

        noteBgHelper = new NoteBgHelper();
        noteBgDataLists = noteBgHelper.queryAll();
        //判断是否设置过背景图片
        if (noteBgDataLists != null && noteBgDataLists.size() > 0) {
            //地址->bitmap->draw
            Drawable drawable = new BitmapDrawable(BitmapFactory.decodeFile(noteBgDataLists.get(0).getPictureKey()));
            //改变布局背景图片
            not_bg_Img.setBackgroundDrawable(drawable);
        }

        adapter = new NoteAdapter();
        note_take_photo_Iv.setOnClickListener(this);

    }

    /****
     * 加载数据
     */
    private void initData() {
        getBitmap(getCurrentUserAvatar(), note_head_pic_Iv);//设置头像
        String username = getIntent().getStringExtra("currentusername");
        currentUsername.setText(username);//设置当前用户名
        usernickname.setText(getCurrentUserNick());//设置当前用户昵称

        helper = new AroundHelper();//初始化Helper
        list = helper.queryAllOrder();
        note_listView.setAdapter(adapter);
        adapter.addData(list);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_take_photo_Iv://拍照发心情
                NoteDialogFragment noteDialogFragment = new NoteDialogFragment();
                noteDialogFragment.show(getSupportFragmentManager(), "dialogfragment");
                break;
            case R.id.activity_note_list_bac_Iv://返回按钮
                finish();
                break;
            case R.id.not_bg_Img://头像背景墙
                NoteBgDialogFragment noteBgDialogFragment = new NoteBgDialogFragment();
                noteBgDialogFragment.show(getSupportFragmentManager(), "noteBgDialogFragment");
                break;
        }
    }

    /***
     * 从缓存内获取用户昵称
     * @return
     */
    private String getCurrentUserNick() {
        return PreferenceManager.getInstance().getCurrentUserNick();
    }

    /***
     * 从缓存内获取用户头像
     * @return
     */
    private String getCurrentUserAvatar() {
        return PreferenceManager.getInstance().getCurrentUserAvatar();
    }


    /***
     * 图片请求
     *
     * @param bitmap
     * @param imageView
     */
    private void getBitmap(String bitmap, final ImageView imageView) {
        volleySingleton.getImageResuest(bitmap, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }

}
